package web2023.team.homework_10_aop.dao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import web2023.team.homework_10_aop.entity.User;

@Repository
public class UserDaoImpl implements UserDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public JdbcTemplate getJdbcTemplate() {
        return jdbcTemplate;
    }
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }


    @Override
    public User userLogin(User user) {
        if(jdbcTemplate == null){
            System.out.println("jdbc空");
        }
        String sql="select * from user where userName=?";
        BeanPropertyRowMapper<User> rowMapper = new BeanPropertyRowMapper<>(User.class);
        User resUser;
        try{
            resUser = jdbcTemplate.queryForObject(sql,rowMapper,user.getUserName());
        }catch (DataAccessException e) {
            return null;
        }

        return resUser;
    }



}
